#include "../include/head.h"

int user_registration(MYSQL *mysql, const char *name, const char *setting, const char *ciphertest,int *user_path_id,int *user_id)
{
    // 先查询用户名是否与已在数据库里的用户重复
    char buf[1024] = {0};
    sprintf(buf, "select * from user_data where user_name = '%s';", name);
    int query_ret = mysql_query(mysql, buf);
    if(query_ret != 0)
    {
        fprintf(stderr, "%s:%s\n", "check_user_name:", mysql_error(mysql));
        return -1;
    }
    // 获取读取到的行数，如果读取到（row == NULL）则说明有同名用户
    MYSQL_RES *table = mysql_store_result(mysql);
    // 读出第一行
    MYSQL_ROW row = mysql_fetch_row(table);
    if(row != NULL)
    {
        return -2;
    }
    mysql_free_result(table);

    // 走到这说明无同名用户，同意注册
    // 新建文件表项
    bzero(buf,sizeof(buf));
    sprintf(buf, "insert into fd_data (file_dir_name,user_name,p_id,file_dir_type,path,is_delete) values ('%s','%s',-1,'%d','/%s','0');",name, name, is_Dir, name);
    int query_ret2 = mysql_query(mysql, buf);
    if(query_ret2 != 0)
    {
        fprintf(stderr, "%s:%s\n", "insert_FD_data:", mysql_error(mysql));
        return -1;
    }
    // 查找新建的文件表项，获取user_path_id
    bzero(buf,sizeof(buf));
    sprintf(buf, "select * from fd_data where user_name = '%s';", name);
    int query_ret3 = mysql_query(mysql, buf);
    if(query_ret3 != 0)
    {
        fprintf(stderr, "%s:%s\n", "get_user_path_id:", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *table2 = mysql_store_result(mysql);
    // 读出第一行，获取user_path_id
    MYSQL_ROW row2 = mysql_fetch_row(table2);
    *user_path_id = atoi(row2[0]);
    printf("%d\n", *user_path_id);

    // 新建用户表项
    bzero(buf,sizeof(buf));

    sprintf(buf, "insert into user_data (user_name,user_salt,user_password,home_id,is_delete) values ('%s','%s','%s','%d','0');", name, setting, ciphertest, *user_path_id);
    int query_ret4 = mysql_query(mysql, buf);
    if(query_ret4 != 0)
    {
        fprintf(stderr, "%s:%s\n", "insert_user_data:", mysql_error(mysql));
        return -1;
    }
    // 查找新建的用户表项，获取user_id
    bzero(buf,sizeof(buf));
    sprintf(buf, "select * from user_data where user_name = '%s';", name);
    int query_ret5 = mysql_query(mysql, buf);
    if(query_ret5 != 0)
    {
        fprintf(stderr, "%s:%s\n", "get_user_id:", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *table3 = mysql_store_result(mysql);
    // 读出第一行，获取user_path_id
    MYSQL_ROW row3 = mysql_fetch_row(table3);
    *user_id = atoi(row3[0]);
    printf("%d\n", *user_id);

    return 0;
}

int user_login1(MYSQL *mysql,const char *name,char *setting)
{
    // 在数据库中按用户名查找，看该用户是否存在
    char buf[1024] = {0};
    sprintf(buf, "select * from user_data where user_name = '%s';", name);
    int query_ret = mysql_query(mysql, buf);
    if(query_ret != 0)
    {
        fprintf(stderr, "%s:%s\n", "mysql query", mysql_error(mysql));
        exit(-1);
    }
     // 获取得到的用户表项
    MYSQL_RES *table = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(table);
    if(row == NULL)
    {
        return -1;
    } 
    memcpy(setting, row[2], strlen(row[2]));
    return 0;
}

int user_login2(MYSQL *mysql, const char *name, const char *ciphertest,int *user_path_id,int *user_id)
{
    // 在数据库中按用户名查找，得到用户曾经存储的密码密文
    char buf[1024] = {0};
    sprintf(buf, "select * from user_data where user_name = '%s';", name);
    int query_ret = mysql_query(mysql, buf);
    if(query_ret != 0)
    {
        fprintf(stderr, "%s:%s\n", "mysql query", mysql_error(mysql));
        exit(-1);
    }
    // 获取得到的用户表项
    MYSQL_RES *table = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(table);
    if (strcmp(row[3], ciphertest) == 0)
    {
        *user_path_id = atoi(row[4]);
        *user_id = atoi(row[0]);
        return 0;
    }
    else
    {
        return -1;
    }
}